import Vue from 'vue'
import store from '@/store/index'
import VueRouter from 'vue-router'
import Layout from '@/layout'
import NotFound from '@/views/404'
// import VueRouter from 'vue-router/types'

Vue.use(VueRouter)

// 页面刷新时，重新赋值token
if (sessionStorage.getItem('token')) {
  store.commit('set_token', sessionStorage.getItem('token'))
}

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/',
    redirect: '/user',
    component: Layout,
    meta: { title: '用户管理', icon: 'user' },
    children: [
        {
            path: '/user',
            name: 'user',
            component: () => import('@/views/user/index'),
            meta: { title: '用户信息'},
        },
        {
          path: '/Recharge',
          name: 'Recharge',
          hidden: true,
          component: () => import('@/views/user/Recharge'),
          meta: { title: '充值记录' },
      },
        {
            path: '/userStatistics',
            name: 'userStatistics',
            component: () => import('@/views/user/statistics'),
            meta: { title: '用户统计'}
        },
        {
            path: '/collection',
            name: 'collection',
            component: () => import('@/views/user/collection'),
            meta: { title: '收款设置'}
        },
        {
            path: '/order',
            name: 'order',
            component: () => import('@/views/user/order'),
            meta: { title: '我的订单' },
            children:[
                {
                    path: '/addOrder',
                    name: 'addOrder',
                    hidden: true,
                    component: () => import('@/views/user/addOrder'),
                    meta: { title: '新增订单' },
                }
            ]
        },
    ]
  },
  
  {
    path: '/currency',
    component: Layout,
    meta: { title: '币种管理', icon: 'dashboard' },
    children: [
        {
            path: '/currencyInfo',
            name: 'currencyInfo',
            component: () => import('@/views/dashboard/index'),
            meta: { title: '币种信息' },
        }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index')
  },
  {
    path: '/pubIndex',
    name: 'pubIndex',
    component: () => import('@/views/pubLogin/pubIndex')
  },
  {
    path: '*',
    component: NotFound
  }

]

export const asyncRoutes = [

]

const createRouter = () => new VueRouter({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

// createRouter.beforeEach((to, from, next) => {
//   if (to.matched.some(r => r.meta.requireAuth)) {   //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
//     if (store.state.token) {
//       next();
//     } else {
//       next({
//         path: '/',
//         query: {redirect: to.fullPath}
//       })
//
//     }
//   } else {
//     next();
//   }
// })

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
const router = createRouter()

export default router
